import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from "@nestjs/common";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { ApiResponse } from "../exception/api.exception";
import { ApiErrorCode } from "../../ShareModule/api-error-code";

@Injectable()
export class ApiInterceptors implements NestInterceptor{
    intercept(
        context:ExecutionContext,
        next:CallHandler
    ):Observable<any>{
        //获取到response对象
        //const response = context.switchToHttp().getReponse();

         //获取到request对象
        //const 获取到request对象 = context.switchToHttp().getRequest();

        return next.handle().pipe(
            map((data) => new ApiResponse(data || null, ApiErrorCode.SUCCESS,"请求成功"))
        );
    }
}